package com.example.mvvmapplication.viewmodel

import android.os.Looper
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.mvvmapplication.api.ApiException
import com.example.mvvmapplication.domain.MapData
import com.example.mvvmapplication.model.OnSellModel
import kotlinx.coroutines.launch

/**
 *
 * @ClassName:      OnSellViewModel$
 * @Description:    java类作用描述
 * @Author:         xinya
 * @CreateDate:     2020/11/3$ 14:37$
 * @UpdateRemark:   更新内容
 * @Version:        1.0
 */

class OnSellViewModel : ViewModel() {
    private val onSellModel by lazy {
        OnSellModel()
    }

    enum class LoadState {
        None, Loading, Error, Success,
        LOADER_MORE_LOADING,
        LOADER_MORE_EMPTY,
        LOADER_MORE_SUCCESS,
        LOADER_MORE_ERROR,
    }


    // 当前页
    private var currentPage = DEFAULT_PAGE
    val contentList = MutableLiveData<MutableList<MapData>>()
    val loadState = MutableLiveData<LoadState>()

    init {
        loadState.postValue(LoadState.None)
    }

    companion object {
        private const val TAG = "OnSellViewModel"
        private const val DEFAULT_PAGE = 1
    }

    /**
     * 加载更多内容
     */
    fun loadMore() {
        loadState.postValue(LoadState.LOADER_MORE_LOADING)
        currentPage++
        this.listContentByPage(currentPage)
    }

    /**
     * 加载首页内容
     */
    fun loadContent() {
        loadState.postValue(LoadState.Loading)
        this.listContentByPage(currentPage)
    }

    private fun listContentByPage(page: Int) {

        viewModelScope.launch {
            Log.d(TAG, "listContentByPage: " + (Looper.getMainLooper() == Looper.myLooper()))

            try {
                val onSellList = onSellModel.getOnSellList(page)
                if (loadState.value == LoadState.LOADER_MORE_LOADING) {
                    val value = contentList.value
                    value?.addAll(onSellList.tbk_dg_optimus_material_response.result_list.map_data)
                    contentList.postValue(value)

                } else {
                    contentList.postValue(onSellList.tbk_dg_optimus_material_response.result_list.map_data)
                }
                contentList.postValue(onSellList.tbk_dg_optimus_material_response.result_list.map_data)
                loadState.postValue(LoadState.Success)
            } catch (e: ApiException) {
                Log.d(TAG, "listContentByPage: " + e.code + " --> " + e.message)
            } catch (e: Exception) {
                // TODO: 2020/11/3 判断错误类型
                Log.e(
                    TAG, "listContentByPage: " +
                            e.printStackTrace()
                )
                loadState.postValue(
                    if (loadState.value == LoadState.LOADER_MORE_LOADING)
                        LoadState.LOADER_MORE_ERROR
                    else LoadState.Error
                )
            }

        }
    }


}